package lanou3g.myfirstapp36kr.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import lanou3g.myfirstapp36kr.MyUser;
import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.base.BaseActivity;

/**
 * Created by dllo on 16/9/5.
 */
public class NickActivity extends BaseActivity implements OnClickListener {
    private ImageView imageViewBack;
    private TextView textViewTitle;
    private Button buttonSave;
    private EditText editTextWrite;
    private MyUser myUser;
    @Override
    protected int getLayout() {
        return R.layout.activity_nick;
    }

    @Override
    protected void initView() {
        imageViewBack = (ImageView) findViewById(R.id.iv_back_nick);
        textViewTitle = (TextView) findViewById(R.id.tv_nick);
        buttonSave = (Button) findViewById(R.id.btn_save);
        editTextWrite = (EditText) findViewById(R.id.tv_write);
        myUser = BmobUser.getCurrentUser(MyUser.class);

    }

    @Override
    protected void initData() {
        imageViewBack.setOnClickListener(this);
        buttonSave.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back_nick:
                finish();
                break;
            case R.id.btn_save:
                String stringWrite = editTextWrite.getText().toString();
                Intent intent = new Intent();
                //if (BuildConfig.DEBUG) Log.d("PlacesActivity", cityPicker.getCity_string());
                //上传网络数据地址
                if (myUser != null){
                    myUser.setNick(stringWrite);
                    myUser.update(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if(e == null){
                                Log.d("MyFragment", "上传成功");
                            }else {
                                Log.d("MyFragment", "上传失败:" + e.getMessage());
                            }
                        }
                    });

                }else {
                    Log.d("MyFragment", "myUser == null");
                }
                intent.putExtra("stringWrite",stringWrite);
                this.setResult(97,intent);
                finish();
                break;
        }

    }
}
